<?php

/**
**名称:手机购物集子管理
**作用:手机购物集子管理功能
**作者:张剑 swordphp@126.com
**创建日期:2013-06-06
**最后修改日期:2013-07-06
**/
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
class jihe_manage extends admin{
	public function __construct(){
		parent::__construct();
		$this ->img_dir_to = PHPCMS_PATH."swwwroot/statics/images/".date("m-d")."/";
		$this ->img_url = "/statics/images/".date("m-d")."/";
		pc_base::load_app_func('global');
		pc_base::load_sys_func('dir');
		$this -> jihe_db = pc_base::load_model('jihe_model');
		$this ->tree = pc_base::load_sys_class('tree');
	}
	public function init(){
		$big_menu = array("?m=tmall_zhekou&c=jihe_manage&a=add&pid=$_GET[list_pid]&list_pid=$_GET[list_pid]", '添加');
		$list_pid = isset($_GET['list_pid'])?$_GET['list_pid']:'0';
		$result = $this->jihe_db ->select("",'*','','order_id ASC');
		$this->tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
		$this->tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		foreach($result as $r) {
			$r['cname'] = $r['classname'];
			$r['ji_manage'] = '<a href ="?m=tmall_zhekou&c=jihe_manage&a=re_data&j_id='.$r['id'].'">更新数据</a> | <a href ="?m=tmall_zhekou&c=static_page&a=init&j_id='.$r['id'].'">生成页面</a> | <a href ="?m=tmall_zhekou&c=jihe_data&a=init&j_id='.$r['id'].'">数据管理</a>';
			$r['str_manage'] = '<a href ="?m=tmall_zhekou&c=jihe_manage&list_pid='.$list_pid.'&a=add&pid='.$r['id'].'">添加</a> | <a href ="?m=tmall_zhekou&c=jihe_manage&list_pid='.$list_pid.'&pid='.$_GET['pid'].'&a=edit&id='.$r['id'].'&j_id='.$r['id'].'">修改</a> ';
			/* <a href="javascript:confirmurl(\'?m=gw_pagemanage&c=gw_pagemanage&a=del&list_pid='.$list_pid.'&id='.$r['id'].'&menuid='.$_GET['menuid'].'\',\''.L('confirm',array('message'=>$r['cname'])).'\')">删除</a>*/
			$r['parentid'] = $r['pid'];
			$array[] = $r;
		}
		$str  = "<tr class='mytr' hid='0' pid='\$pid' sid='\$id'>
					<td><input name='order_id[\$id]' type='text' size='3' value='\$order_id' class='input-text-c'></td>
					<td align='center'>\$id</td>
					<td class='clicktrue'>\$spacer\$cname</td>
					<td >\$short_name</td>
					<td ><a href='?m=tmall_zhekou&c=jihe_manage&a=init&list_pid=\$id' target='_blank' >新窗口展开</a></td>
					<td align='center'>\$ji_manage</td>
					<td align='center'>\$str_manage</td>
				</tr>";
		$this->tree->init($array);
		$categorys = $this->tree->get_tree($list_pid, $str);
		include $this->admin_tpl('jihe_manage');
	}
	public function add(){
		if($_POST['submit'] == 'dosubmit'){
			$listpid = isset($_GET['list_pid'])?$_GET['list_pid']:0;
			$res = $this->img_copy();
			$data = $_POST['info'];
			$re_str = array('/\n\r/','/\r/','/\n/');
			$temp = explode('|',preg_replace($re_str,"|",$data['attr']));
			$data['attr'] = array();
			foreach($temp as $attr){
				if(trim($attr) == '') continue;
				$k = explode(':',$attr);
				$data['attr'][$k[0]] = $k[1];
			}
			$data['attr'] = serialize($data['attr']);
			if(is_array($res)&&$res!=''){
				$data['img_url'] = $res['img_url'];
			}
			$r = $this->jihe_db->insert($data);
			if($r){
				showmessage(L('添加成功'),"?m=tmall_zhekou&c=jihe_manage&a=init&list_pid=$listpid");
			}else{
				showmessage(L('添加失败'),'?m=tmall_zhekou&c=jihe_manage&a=init');
			}
		}else{
			$pid = $_GET['pid'];
			$listpid = isset($_GET['list_pid'])?$_GET['list_pid']:0;
			$sort_tree = $this->get_sort_tree($pid);
			$template_list = $this->get_template_list();
			include $this->admin_tpl('info_edit');
		}
	}
	public function edit(){
		if($_POST['submit'] == 'dosubmit'){
			$res = $this->img_copy();
			$data = $_POST['info'];
			$id = $_POST['id'];
			$listpid = isset($_GET['list_pid'])?$_GET['list_pid']:'';
			if($id == ''){
				showmessage(L('修改失败'),'?m=tmall_zhekou&c=jihe_manage&a=init');
			}
			if(is_array($res)&&$res!=''){
				$data['img_url'] = $res['img_url'];
			}
			$r = $this->jihe_db->update($data,array('id'=>$id));
			if($r){
				showmessage(L('修改成功'),"?m=tmall_zhekou&c=jihe_manage&a=init&list_pid=$listpid");
			}else{
				showmessage(L('修改失败'),'?m=tmall_zhekou&c=jihe_manage&a=init');
			}
		}else{
			$id = isset($_GET['id'])?$_GET['id']:'';
			$listpid = isset($_GET['list_pid'])?$_GET['list_pid']:'';
			$info = $this->jihe_db->get_one(array('id'=>$id));
			if($id == ''){
				exit('错误的参数');
			}
			$sort_tree = $this->get_sort_tree($info['pid']);
			$template_list = $this->get_template_list();
			include $this->admin_tpl('info_edit');
		}
	}
	public function del(){
		$listpid = isset($_GET['list_pid'])?$_GET['list_pid']:0;
		$id = isset($_GET[id])?$_GET['id']:'';
		if($id == ''){
			exit('错误的参数');
		}else{
			$res = $this->jihe_db->delete(array('id'=>$id));
			if($res){
				showmessage(L('删除成功'),"?m=tmall_zhekou&c=jihe_manage&a=init&list_pid=$listpid");
			}else{
				showmessage(L('删除失败'),'?m=tmall_zhekou&c=jihe_manage&a=init');
			}
		}
	}
	public function listorder() {

		if(isset($_POST['dosubmit'])) {
			foreach($_POST['order_id'] as $id => $order) {
				$this->jihe_db->update(array('order_id'=>$order),array('id'=>$id));
			}
			showmessage(L('operation_success'));
		} else {
			showmessage(L('operation_failure'));
		}
	}
	final private function img_copy(){
		$allow_ext = array('jpg','png','gif');
		foreach($_FILES as $key=>$val){
			if($val['error'] ==0){
				$tmp = pathinfo($val['name']);
				$ext = $tmp['extension'];
				if(in_array($ext,$allow_ext)){
					//确定文件的拓展名
					$name = date('his').rand(10-99);
					if(!is_dir($this->img_dir_to)){
						$r = dir_create($this->img_dir_to);//如果没有目录则建立目录
					}
					$res = move_uploaded_file($val['tmp_name'], $this->img_dir_to.$name.'.'.$ext);
					if($res){
						return array($key=>$this->img_url.$name.'.'.$ext);
					}
				}else{
					return 'not allow imgextension or its not img';
				}
			}elseif($val['error']!=0){
				continue;
			}
		}
	}
	private function get_template_list(){
		$dir_path = PC_PATH.'templates/default/s_gouwu/';
		$dir_list = dir_list($dir_path);
		foreach($dir_list as $key=>$val){
			$res[$key] = str_replace($dir_path,'',$val);
			$res[$key] = str_replace('.html','',$res[$key]);
		}
		return $res;
	}
	private function get_sort_tree($pid){
		$result = $this->jihe_db->select('','*','','order_id ASC');
		$this->tree->icon = array('&nbsp;│ ','&nbsp;├─ ','&nbsp;└─ ');
		$this->tree->nbsp = '&nbsp;';
		foreach($result as $r) {
			$r['cname'] = $r['classname'];
			$r['parentid'] = $r['pid'];
			$array[] = $r;
		}
		$str  = "<option value=\$id \$selected>\$spacer\$cname</option>";
		$this->tree->init($array);
		$sort_tree = $this->tree->get_tree(0, $str,$pid);
		return $sort_tree;
	}
}